Skip to content

104 maximum depth of binary tree#21

Open
kitano-kazuki wants to merge 5 commits intomainfrom
104-maximum-depth-of-binary-tree
Open

104 maximum depth of binary tree#21
kitano-kazuki wants to merge 5 commits intomainfrom
104-maximum-depth-of-binary-tree

Conversation

@kitano-kazuki
Copy link
Copy Markdown
Owner

frontier.append(root)
depth = 0
while frontier:
num_cur_frontiers = len(frontier)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)

また、 frontiers を単数にするか複数にするかについては、こちらのコメントをご参照ください。
achotto/arai60#6 (comment)

また、 current という単語については、こちらのコメントをご参照ください。
Mike0121/LeetCode#7 (comment)

自分なら num_nodes_in_level と名付けると思います。

Comment on lines +179 to +190
frontier = deque()
frontier.append(root)
depth = 0
while frontier:
num_cur_frontiers = len(frontier)
depth += 1
for _ in range(num_cur_frontiers):
node = frontier.popleft()
if node is None:
continue
frontier.append(node.left)
frontier.append(node.right)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

キューに異なる階層のノードが入っていて、かつそれらの深さはキュー外で管理されている、という構造がちょっと違和感がありました。私なら、いま見る階層と、次に見る階層を分けてスワップします。dequeが不要になるのもメリットです。

Suggested change
frontier = deque()
frontier.append(root)
depth = 0
while frontier:
num_cur_frontiers = len(frontier)
depth += 1
for _ in range(num_cur_frontiers):
node = frontier.popleft()
if node is None:
continue
frontier.append(node.left)
frontier.append(node.right)
frontier = [root]
depth = 0
while frontier:
depth += 1
next_frontier = []
for node in frontier:
if node is None:
continue
next_frontier.append(node.left)
next_frontier.append(node.right)
frontier = next_frontier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants